(function ($) {
    $.topicList = function () {
        return {	//书本基本信息
            pageSize: 10,
            pageIndex: 0,
            bookStatus: 0,
            pageParam: {},
            init: function () {
                this.listener();
                this.initLayui();
                this.resourceList();
            },
            /**
             * 添加各按钮所需要的监听事件
             */
            listener: function () {
                $("#searchInput").keydown(function () {
                    if (event.keyCode == 13) {
                        $.topicList.resourceList();
                    }
                });
                $("#personSearchBtn").click(function () {
                    $.topicList.resourceList();
                });
                /**
                 *点击排序
                 * **/
                $(".topicList").on("click", ".sort", function () {
                    if ($(this).attr("data-isasc") == "2") {
                        $(this).attr("data-isasc", "1");
                        $(this).find(".icon").css("background-image", "url('img/ltb.png')");
                    } else {
                        $(this).attr("data-isasc", "2");
                        $(this).find(".icon").css("background-image", "url('img/btl.png')");
                    }
                    $.topicList.resourceList();
                });
                /**
                 *页面调整
                 * **/
                $.topicList.resize();
                $(window).resize($.topicList.resize);
                /**
                 * 监听hover事件
                 */
                $(".topicList").on("mouseover mouseout", "tbody tr", function (event) {
                    if (event.type == "mouseover") {
                        //鼠标悬浮
                        $(this).find(".controlButtons").css("display", "inline-block");
                    } else if (event.type == "mouseout") {
                        //鼠标离开
                        $(this).find(".controlButtons").css("display", "none");
                    }
                });
                /**
                 * 点击确定修改
                 */
                $(".topicList").on("click", ".dataNameEdit", function (event) {
                    event.stopPropagation();
                });
                $(".topicList").on("click", ".dataNameEdit .icon.confirm", function (event) {
                    event.stopPropagation();
                    $.topicList.inputEdit($(this).prev().find("input"));
                });
                /**
                 * 监听input变化
                 */
                $(".topicList").on("blur", "input.editNameInput", function () {
                    var other = $(this).parents("tr");
                    setTimeout(function () {
                        other.find(".dataNameEdit").css("display", "none");
                        other.find(".names").css("display", "inline-block");
                    }, 500);
                    $(this).parents(".topicList").on("mouseover", "tbody tr", function () {
                        $(this).find(".controlButtons").css("display", "inline-block");
                    });
                });
                $(".topicList").on("keydown", "input.editNameInput", function () {
                    if (event.keyCode == 13) {
                        $(this).parents("tr").find(".dataNameEdit").css("display", "none");
                        $(this).parents(".topicList").on("mouseover", "tbody tr", function () {
                            $(this).find(".controlButtons").css("display", "inline-block");
                        });
                        $.topicList.inputEdit($(this));
                    }
                });
                /**
                 * 点击编辑资料名称
                 */
                $(".topicList tbody").on('click', ".editor", function (event) {
                    event.stopPropagation();
                    $(this).parent().css("display", "none");
                    $(this).parents(".dataCenterList").off("mouseover");
                    $(this).parent().prev().css("display", "none");
                    $(this).parents("tr").find(".dataNameEdit").css("display", "inline-block");
                    $(this).parents("tr").find(".dataNameEdit input").val($(this).parent().parent().find(".resourceName").html()).focus();
                });
                /**
                 * 课件删除一行
                 */
                $(".topicList tbody").on('click', ".delete", function (event) {
                    event.stopPropagation();
                    var topicDelete = {
                        "ids": [
                            $(this).parents("tr").attr('data-id')
                        ],
                        "token": User.token
                    };
                    topicDelete = JSON.stringify(topicDelete);
                    $.topicList.deleteFile(topicDelete, Doman.topicRevocation);
                });
                /**
                 * 课件删除选中行
                 */
                $(".deleteSel").on('click', function () {
                    var inputs = $(".topicList").find("tbody input[type='checkbox']:checked");
                    var ids = [];
                    if (inputs.length > 0) {
                        $.each(inputs, function (i, inputDom) {
                            ids.push($(inputDom).parents("tr").attr('data-id'));
                        });
                        var topicDelete = {
                            "ids": ids,
                            "token": User.token
                        };
                        topicDelete = JSON.stringify(topicDelete);
                        $.topicList.deleteFile(topicDelete, Doman.topicRevocation);
                    }else{
                        layer.msg("请点击选中项再删除");
                    }

                });

                /**
                 * 点击加入到
                 */
                $(".topicList tbody").on('click', ".addTo", function (event) {
                    event.stopPropagation();
                    var topicImpotDTOList = [
                        {
                            "topicGroupId": $(this).parents("tr").attr("data-id"),
                            "sequence": 0
                        }
                    ];
                    $.topicList.topicImport(topicImpotDTOList);
                });
                /**
                 * 点击选择框 加入到
                 */
                $(".addToSel").on('click', function () {
                    var inputs = $(".topicList").find("tbody input[type='checkbox']:checked");
                    var topicImpotDTOList = [];
                    if(inputs.length>0){
                        $.each(inputs, function (i, inputDom) {
                            var obj = {};
                            obj.topicGroupId = $(inputDom).parents("tr").attr("data-id");
                            obj.sequence = 0;
                            topicImpotDTOList.push(obj);
                        });
                        $.topicList.topicImport(topicImpotDTOList);
                    }else{
                        layer.msg("请点击选中项再加入到");
                    }
                });
                /**
                 * 点击解散
                 */
                $(".topicList tbody").on('click', '.dissolve',function (event) {
                    event.stopPropagation();
                    var topicSplit={
                        "topicGroupId": $(this).parents("tr").attr("data-id"),
                        "token": User.token
                    };
                    updateObject(Doman.url + Doman.topicSplit, true, JSON.stringify(topicSplit), function (result) {
                        $.topicList.resourceList(true);
                    }, function () {
                    });
                });
                /**
                 * 点击合并
                 */
                $(".merge").on('click',function () {
                    var inputs = $(".topicList").find("tbody input[type='checkbox']:checked");
                    var topicGroupIdList = [];
                    if(inputs.length>0){
                        $.each(inputs, function (i, inputDom) {
                            topicGroupIdList.push($(inputDom).data("id"));
                        });
                        var topicCombine={
                            "topicGroupIdList": topicGroupIdList,
                            "token": User.token
                        };
                        updateObject(Doman.url + Doman.topicCombine, true, JSON.stringify(topicCombine), function (result) {
                            $.topicList.resourceList(true);
                        }, function () {
                        });
                    }else{
                        layer.msg("请点击选中项再合并");
                    }
                });

                /**
                 * 点击创建题目
                 */
                $(".createTopic").on('click', function () {
                    openUrl(window.location.href,"AddTopic.html","添加题目");
                    //对面包屑进行编辑
                    var html='<a data-href="AddTopic.html">添加题目</a>';
                    $(parent.document).find(".layui-breadcrumb a:last-child").append('<span class="layui-box">&gt;</span>');
                    $(parent.document).find(".layui-breadcrumb").append(html);
                });
                /**题目详情页跳转**/
                $(".topicList tbody").on('click', ".topicIcon,.names", function (){
                    var topicGroupId =$(this).parents("tr").attr("data-id");
                    sessionStorage.setItem('topicGroupId',topicGroupId);
                    var topicDetailName=$(this).data("index")+"-"+$(this).data("total");
                    openUrl( window.location.href,'TopicManageDetail.html',topicDetailName);
                    sessionStorage.setItem('topicDetailName',topicDetailName);
                    //对面包屑进行编辑
                    var html='<a data-href="TopicManageDetail.html">题目详情</a>';
                    $(parent.document).find(".layui-breadcrumb a:last-child").append('<span class="layui-box">&gt;</span>');
                    $(parent.document).find(".layui-breadcrumb").append(html);
                });
                //阻止冒泡跳转
                $(".topicList tbody").on("click",".layui-form-checkbox,.control",function(event){
                    event.stopPropagation();
                });
            },
            /**
             * 页面设置高度
             * **/
            resize: function () {
                $(".topicListContent").css("height", window.innerHeight - 129 + "px");
            },
            /**
             * input修改数据
             * data 数据
             * **/
            inputEdit: function (othis) {
                othis.parents("tr").find(".dataName").css("display", "inline-block");
                othis.parents("tr").find(".dataName").next().css("display", "inline-block");
                if ($.trim(othis.val()) != "") {
                    othis.parents("tr").find(".dataName").html(othis.val());
                    var topicUpdateDes = {
                        "topicGroupId": othis.parents("tr").data("id"),
                        "description": othis.val(),
                        "token": User.token
                    };
                    $.topicList.topicUpdateDes(topicUpdateDes);
                    $.topicList.resourceList(true);
                } else {
                    layer.msg("资料名称不可为空", {icon: 2});
                }
            },
            /**
             * 提交数据修改名字
             * data 数据
             * **/
            topicUpdateDes: function (data) {
                updateObject(Doman.url + Doman.topicUpdateDes, false, JSON.stringify(data), function (result) {
                    if (result.code == 200) {
                        layer.msg(result.message, {icon: 1});
                    } else {
                        layer.msg(result.message, {icon: 2});
                    }
                }, function () {
                });
            },
            /**
             * 加入到课程中
             * courseWareImportDTOList 数据
             * unitId 数据
             * **/
            topicImport: function (data) {
                //iframe窗
                layer.open({
                    type: 2,
                    title: "自建课程",
                    //closeBtn: 0, //不显示关闭按钮
                    area: ['900px', window.innerHeight - 100 + "px"],
                    content: "pageFrame/selfList.html"
                    , btn: ['确认']
                    , yes: function (index, layero) {
                        var input = layer.getChildFrame("input[type='radio']:checked", index);
                        var unitId = $(input).data("unitid");
                        var topicImpotDTOList = {
                            "topicImpotDTOList": data,
                            "unitId": unitId,
                            "token": User.token
                        };
                        updateObject(Doman.url + Doman.topicImport, true, JSON.stringify(topicImpotDTOList), function (result) {
                            $.topicList.resourceList(true);
                        }, function () {
                        });
                        layer.close(index);
                    }
                });
            },
            /**
             * 删除确认弹出框
             * data 数据
             * url 地址
             * **/
            deleteFile: function (data, url) {
                layer.confirm('是否删除', {
                    btn: ['确定', '取消'] //按钮
                }, function (index, layero) {
                    updateObject(Doman.url + url, true, data, function (result) {
                        $.topicList.resourceList(true);
                        layer.close(index);
                    }, function () {
                    });
                }, function () {
                });
            },
            initLayui: function () {
                layui.use(['layer', 'form', 'laypage', 'table'], function () {
                     $ = layui.jquery, layer = (parent.layer === undefined) ? layui.layer : parent.layer,form = layui.form, table = layui.table, laypage = layui.laypage;
                    form.render('checkbox');
                    //全选
                    form.on('checkbox(allChoose)', function (data) {
                        var child = $(data.elem).parents('#topicList').find('.topicList input[type="checkbox"]');
                        child.each(function (index, item) {
                            item.checked = data.elem.checked;
                        });
                        form.render('checkbox');
                    });
                });
            },
            resourceList: function (ifPage) {
                if (!ifPage) {
                    $.topicList.pageIndex = 0;
                }
                var submitData = {
                    isASC: $(".sort").attr("data-isasc"),
                    content: $("#searchInput").val(),
                    page: $.topicList.pageIndex,
                    pageSize: $.topicList.pageSize,
                    token: User.token
                };
                updateObject(Doman.url + Doman.topicList, true, JSON.stringify(submitData), function (result) {
                    //设置共多少数据
                    $(".totalNum span").html(result.totalElements);
                    var html = "";
                    if (result.code=="200"&&result.topicList.length> 0) {
                        $.each(result.topicList, function (i, data) {
                            var index=$.topicList.pageIndex*$.topicList.pageSize+(i+1);
                            html+='<tr data-index='+index+' data-total='+data.topicNum+' data-id='+data.topicGroupId+'>'
                                + '    <td>'
                                + '       <input data-id='+data.topicGroupId+' type="checkbox" name="" lay-skin="primary" title='+index+'>'
                                + '    </td>'
                                + '    <td>'
                                + '        <span class="topicIcon"></span>'
                                + '        <div class="names">'
                                + '            <span>'+data.topicNum+'题</span>'
                                + '            <span class="resourceName">'+(data.title==undefined?"":data.title)+'</span>'
                                + '        </div>'
                                + '        <div class="controlButtons">';
                            if(data.topicNum>1){
                                html+='         <span class="icon dissolve"></span>'
                            }
                            html+= '            <span class="icon editor"></span>'
                                +  '            <span class="icon addTo"></span>'
                                +  '            <span class="icon delete"></span>'
                                +  '        </div>'
                                +  '        <div class="layui-form-item dataNameEdit">'
                                +  '            <div class="layui-input-inline">'
                                +  '                <input maxlength="12" autofocus="" type="text" name="title" lay-verify="title" autocomplete="off" class="layui-input editNameInput">'
                                +  '            </div>'
                                +  '            <span class="icon confirm"></span>'
                                +  '            <span class="icon cancel"></span>'
                                +  '         </div>'
                                + '    </td>'
                                + '    <td>' + changeDate(data.createTime) + '</td>'
                                + '</tr>';
                        });
                        $(".topicList tbody").html(html);
                        $.topicList.initLayui();
                    }else{
                        if($.topicList.pageIndex!="0"){
                            $.topicList.pageIndex=$.topicList.pageIndex-1;
                            $.topicList.resourceList(true);
                            return;
                        }else{
                            $(".topicList tbody").html("");
                            $.topicList.initLayui();
                        }
                    }
                    $.topicList.initPage(result.totalElements);
                }, function () {
                });
                $.topicList.inputEmpty();
            },
            inputEmpty:function(){
                //引用完成后input全置为空
                $("input[type='checkbox']").attr("checked",false);
                $.each($("input[type='checkbox']"),function(i,inputs){
                    $(inputs).attr("checked",false);
                    $.topicList.initLayui();
                });
            },
            /**
             * 初始化分页页码
             * @returns
             */
            initPage: function (total) {
                layui.use(['laypage'], function () {
                    var $ = layui.jquery, layer = layui.layer, laypage = layui.laypage;
                    laypage.render({
                        elem: 'pagination'
                        , count: total //数据总数，从服务端得到
                        , first: '首页'
                        , last: '尾页'
                        , layout: ['prev', 'page', 'next', 'skip', 'limit']
                        , curr: $.topicList.pageIndex+1
                        , limit: $.topicList.pageSize
                        , skip: true
                        , theme: '#337ab7'
                        , jump: function (obj, first) {
                            $.topicList.pageIndex = obj.curr-1;
                            $.topicList.pageSize = obj.limit;
                            //首次不执行
                            if (!first) {
                                $.topicList.resourceList(true);
                            }
                        }
                    });
                });

            }
        }
    }();
    $.topicList.init();
})(jQuery);